package com.zh.book_borrow.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zh.book_borrow.vo.BookVO;
import com.zh.book_borrow.common.PageResult;
import com.zh.book_borrow.entity.Book;
import com.zh.book_borrow.mapper.BookMapper;
import com.zh.book_borrow.service.BookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class BookServiceImpl implements BookService {
    @Autowired
    private BookMapper bookMapper;

    @Override
    public PageResult<BookVO> page(Integer pageNum, Integer pageSize, String borrower, String bookName,Integer state) {
        log.info("pageNum:{},pageSize:{},borrower:{},bookName:{},state:{}", pageNum, pageSize, borrower, bookName,state);
        PageHelper.startPage(pageNum, pageSize);
        List<BookVO> bookVOList = bookMapper.page(pageNum, pageSize, borrower, bookName,state);
        PageInfo<BookVO> pageInfo = new PageInfo<>(bookVOList);
        log.error("total:{}",pageInfo.getTotal());
        return PageResult
                .<BookVO>builder()
                .pageNum(pageInfo.getPageNum())
                .pageSize(pageInfo.getList().size())
                .records(pageInfo.getList())
                .total(pageInfo.getTotal())
                .build();
    }

    @Override
    public void updateState(Long bookId, Long userId, Integer state) {
        bookMapper.updateState(bookId, userId, state);
    }
}
